<template>
  <div class="billList">
    <section class="header">
      <span>订单管理</span>
    </section>
    <div class="search-box" @keyup.enter="searchFun">
      <i class="iconfont iconsearch1"></i>
      <el-input
        placeholder="输入订单号进行搜索"
        class="search"
        v-model.trim="orderNo"
      ></el-input>
      <div class="btn">
        <el-button
          type="primary"
          title="生成订单"
          @click="createOrder()"
          :loading="createOrderLoading"
        >
          生成订单
        </el-button>
        <el-button
          type="success"
          title="请选择要导出的订单"
          :disabled="disposeArr.length <= 0"
          :loading="outInutLoading"
        >
          导出
        </el-button>
      </div>
    </div>
    <div class="search-box">
      <section class="option">
        <span>订单类型:</span>
        <el-select
          v-model="orderTypeId"
          @change="searchFun"
          placeholder="请选择"
        >
          <el-option
            v-for="item in orderTypeOptions"
            :key="item.orderTypeId"
            :label="item.orderType"
            :value="item.orderTypeId"
          ></el-option>
        </el-select>
      </section>
      <section class="option">
        <span>是否开票:</span>
        <el-select v-model="isInvoice" @change="searchFun" placeholder="请选择">
          <el-option
            v-for="item in isInvoiceOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </section>
      <section class="option">
        <span>订单状态:</span>
        <el-select
          v-model="orderStatus"
          @change="searchFun"
          placeholder="请选择"
        >
          <el-option
            v-for="item in orderStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </section>
    </div>
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        v-loading="loading"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="orderNo" label="订单号"></el-table-column>
        <el-table-column
          prop="orderTypeId"
          label="订单类型"
          :formatter="orderTypeFormatter"
        ></el-table-column>
        <!-- <el-table-column
          prop="balance"
          label="余额"
          :formatter="moneyFilter"
        ></el-table-column> -->
        <el-table-column
          prop="amount"
          label="金额"
          :formatter="moneyFilter"
        ></el-table-column>
        <!-- <el-table-column
          prop="balanceBeforeOperation"
          label="操作前金额"
          :formatter="moneyFilter"
        ></el-table-column> -->
        <el-table-column
          prop="orderStatus"
          label="订单状态"
          :formatter="orderStatusFormatter"
        ></el-table-column>
        <el-table-column
          prop="isInvoice"
          label="是否到账"
          :formatter="isInvoiceFormatter"
        ></el-table-column>
        <el-table-column
          prop="isInvoice"
          label="是否开票"
          :formatter="isInvoiceFormatter"
        ></el-table-column>
        <el-table-column prop="createTime" label="时间"></el-table-column>
        <el-table-column label="详情">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="detail(scope.row.orderId, scope.row.orderNo)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="confirmOrder(scope.row)"
              >发票申请</el-button
            >

            <el-button
              size="mini"
              type="primary"
              @click="confirmOrder(scope.row)"
              >发票开具</el-button
            >
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              :disabled="
                scope.row.orderStatus == 2 || scope.row.orderStatus == 1
              "
              @click="confirmOrder(scope.row)"
              >确认订单</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!--账单对话框-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogTableVisible">
      <ul class="billAmount-box">
        <li class="billAmount">{{ billAmount | moneyFilters }}</li>
        <li>账单合计</li>
      </ul>
      <el-table :data="gridData">
        <el-table-column
          property="userName"
          label="姓名"
          width="200"
        ></el-table-column>
        <el-table-column
          property="yearMonth"
          label="社保月份"
        ></el-table-column>
        <el-table-column
          property="sbMoney"
          label="社保金额"
          :formatter="moneyFilter"
        ></el-table-column>
        <el-table-column
          property="yearMonth"
          label="公积金月份"
        ></el-table-column>
        <el-table-column
          property="gjjMoney"
          label="公积金金额"
          :formatter="moneyFilter"
        ></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogTableVisible = false"
          >确定</el-button
        >
      </div>
    </el-dialog>
    <!--发票信息-->
    <el-dialog
      title="变更发票信息"
      :visible.sync="infoDialog"
      :close-on-click-modal="false"
      :show-close="false"
      center
    >
      <el-form :model="info">
        <el-form-item label="发票类型(必填)" :label-width="formLabelWidth">
          <el-select
            v-model="info.type"
            @change="typeChange"
            placeholder="请选择发票类型"
          >
            <el-option label="普通电子发票" value="1"></el-option>
            <el-option label="不需要发票" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="开票抬头(必填)"
          v-show="isNeed == '1'"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="info.head"
            placeholder="在这里输入开票抬头"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          label="纳税人识别号(必填)"
          v-show="isNeed == '1'"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="info.id"
            placeholder="在这里纳税人识别号"
            clearable
          ></el-input>
        </el-form-item>
        <p class="tip" @click="open" v-show="isNeed == '1'">{{ text }}</p>
        <h4 v-show="isOpen">
          下列信息只有贵司对发票格式有具体要求时才需要填写填写保存后将体现在开具的发票上
        </h4>
        <el-form-item
          label="收票邮箱"
          v-show="isOpen"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="info.email"
            placeholder="在这里输入收票邮箱"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          label="通知手机"
          v-show="isOpen"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="info.phone"
            placeholder="在这里输入通知手机"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          label="特殊开票需求"
          v-show="isOpen"
          :label-width="formLabelWidth"
        >
          <el-select v-model="info.specialType" placeholder="请选择发票类型">
            <el-option label="正常开票" value="正常开票"></el-option>
            <el-option
              label="只开服务费金额"
              value="只开服务费金额"
            ></el-option>
            <el-option
              label="不开服务费金额"
              value="不开服务费金额"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="公司注册地址及电话"
          v-show="isOpen"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="info.phone"
            placeholder="公司注册地址及电话"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          label="开户行及账号"
          v-show="isOpen"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="info.phone"
            placeholder="开户行及账号"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="infoDialog = false">取 消</el-button>
        <el-button type="primary" @click="infoDialog = false">确 定</el-button>
      </div>
    </el-dialog>
    <!--发送账单明细-->
    <el-dialog
      title="主题：2019年09月账单明细"
      :visible.sync="operateDialog"
      center
    >
      <el-form-item label="收件人" :label-width="formLabelWidth">
        <el-input
          v-model="sendEmail"
          placeholder="在这里输入邮箱"
          clearable
        ></el-input>
      </el-form-item>
      <p class="message">您好！这是贵公司2019年09月使用服务的账单明细</p>
      <p class="file">附件: 2019年09月账单明细.xlsx</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="operateDialog = false">取 消</el-button>
        <el-button type="primary" @click="operateDialog = false"
          >发 送</el-button
        >
      </div>
    </el-dialog>
    <!--上传图片-->
    <el-dialog
      title="上传收款凭证"
      :visible.sync="updateDialog"
      :close-on-click-modal="false"
      :show-close="false"
      center
    >
      <div class="img-head">
        <i class="iconfont iconadd1"></i>
        <input type="file" name="" id="" @change="upImg($event)" />
      </div>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
  </div>
</template>

<script>
import base from "@/assets/js/base";
import { requestApiPost, requestApiFilePost } from "../api/api";
import { reqRep } from "../report/reqReport";
import { resRep } from "../report/resReport";
export default {
  name: "billManagement",
  data() {
    return {
      disposeArr: [],
      outInutLoading: false,
      createOrderLoading: false,
      updateDialog: false,
      text: "+ 填写特殊需求",
      isOpen: false,
      infoDialog: false,
      operateDialog: false,
      isNumber: 0,
      switchIndex: 0,
      orderNo: "",
      checked: true,
      current: 1,
      total: null,
      pageSize: 6,
      isNeed: "1",
      dialogTableVisible: false,
      formLabelWidth: "160px",
      orderTypeId: "0",
      orderTypeOptions: [],
      isInvoice: "",
      isInvoiceOptions: [
        {
          label: "全部",
          value: "",
        },
        {
          label: "未开",
          value: "0",
        },
        {
          label: "已开",
          value: "1",
        },
      ],
      orderStatus: "3",
      orderStatusOptions: [
        {
          label: "生成订单",
          value: "0",
        },
        {
          label: "订单处理中",
          value: "1",
        },
        {
          label: "订单完成",
          value: "2",
        },
        {
          label: "全部",
          value: "3",
        },
      ],
      tableData: [],
      orderItems: [],
      info: {
        type: "1",
        specialType: "",
        id: "",
        head: "",
      },
      billAmount: 0,
      dialogTitle: "",
      gridData: [],
      sendEmail: "",
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.disposeArr = val;
    },
    //上传
    upImg(e) {
      requestApiFilePost(e.target.files[0]).then((data) => {
        e.target.value = "";
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          if (!resReport.data.fileId || !resReport.data.filePath) {
            this.$message.warning("上传路径未获取");
            return;
          } else {
            this.orderVoucher = resReport.data.fileId;
            this.orderVoucherPath = resReport.data.filePath;
            this.updateDialog = false;
            this.comfirmPay();
          }
        }
      });
    },
    /**生成订单
     *
     */
    createOrder() {
      this.$confirm("确定生成当前月的订单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.confirmCreateOrder();
        })
        .catch(() => {});
    },
    /**确认生成订单 */
    confirmCreateOrder() {
      var reqData = {};
      this.createOrderLoading = true;
      let reqReport = reqRep({ traCode: "_BUILD_ORDER_1", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.createOrderLoading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.$message({
            message: "生成成功",
            type: "success",
          });
          this.searchFun();
        }
      });
    },
    //确认订单
    comfirmPay() {
      this.loading = true;
      var reqData = {
        orderNo: this.info.orderNo,
        orderType: this.info.orderTypeId,
        orderVoucher: this.orderVoucher,
        orderVoucherPath: this.orderVoucherPath,
      };
      let reqReport = reqRep({ traCode: "_CONFIRM_ORDER", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.$message({
            message: "成功",
            type: "success",
          });
          this.searchFun();
        }
      });
    },
    confirmOrder(info) {
      this.$confirm("您确认收款?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.info = info;
          //充值
          if (this.info.orderTypeId == 1) {
            this.updateDialog = true;
          } else {
            this.orderVoucher = "";
            this.orderVoucherPath = "";
            this.comfirmPay();
          }
        })
        .catch(() => {});
    },
    typeChange(e) {
      this.isNeed = e;
      if (e == "0") {
        this.isOpen = false;
        this.text = "+ 填写特殊需求";
      }
    },
    handleEdit(i, obj) {},
    sendFun() {
      this.operateDialog = true;
    },
    open() {
      if (this.isOpen) {
        this.text = "+ 填写特殊需求";
        this.isOpen = false;
      } else {
        this.text = "- 清空并收起下列选填信息";
        this.isOpen = true;
      }
    },
    operate(i, obj) {
      console.log(obj);
    },
    lookBill() {
      this.dialogTableVisible = true;
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getData();
    },
    handleCurrentChange(e) {
      this.current = e;
      this.getData();
    },
    isClick(num) {
      this.isNumber = num;
    },
    searchFun() {
      this.current = 1;
      this.pageSize = 6;
      this.getData();
    },
    switchFun(i) {
      this.current = 1;
      this.pageSize = 6;
      this.switchIndex = i;
      this.getData();
    },
    switchChange() {},
    getInfo(orderSn) {
      base
        .getAjax("/bill/orderItem", {
          orderSn: orderSn,
        })
        .then((res) => {
          if (res.data.code == 1) {
            this.orderItems = res.data.data;
          } else {
            this.$message({
              message: res.data.message,
              type: "error",
              duration: 1500,
            });
          }
        });
    },
    //获取账单列表
    getData() {
      this.loading = true;
      var reqData = {
        orderTypeId: this.orderTypeId,
        orderNo: this.orderNo,
        isInvoice: this.isInvoice,
        orderStatus: this.orderStatus,
        current: this.current,
        pageSize: this.pageSize,
        companyId: "",
        personId: JSON.parse(sessionStorage.getItem("login")).personId,
      };
      let reqReport = reqRep({ traCode: "_LIST_ORDER", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.tableData = resReport.data.records;
          this.total = resReport.data.total;
        }
      });
    },
    //详情
    detail(orderId, orderNo) {
      this.$router.push({
        path: "/billDetail",
        query: { orderId: orderId, orderNo: orderNo },
      });
    },
    //折叠框
    toogleExpand(row) {
      this.getInfo(row.orderSn);
      let $table = this.$refs.table;
      this.tableData.map((item) => {
        if (row.orderSn != item.orderSn) {
          $table.toggleRowExpansion(item, false);
        }
      });
      $table.toggleRowExpansion(row);
    },
    toogleExpand2(row) {
      this.getInfo(row.orderSn);
      let $table = this.$refs.tables;
      this.tableData.map((item) => {
        if (row.orderSn != item.orderSn) {
          $table.toggleRowExpansion(item, false);
        }
      });
      $table.toggleRowExpansion(row);
    },

    toogleExpand3(row) {
      this.getInfo(row.orderSn);
      let $table = this.$refs.tablex;
      this.tableData.map((item) => {
        if (row.orderSn != item.orderSn) {
          $table.toggleRowExpansion(item, false);
        }
      });
      $table.toggleRowExpansion(row);
    },
    //获取订单类型
    getOrderType() {
      var reqData = {
        orderType: "",
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_ORDER_TYPE", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.orderTypeOptions = resReport.data.records;
          this.orderTypeOptions.unshift({
            orderType: "全部",
            orderTypeId: "0",
          });
        }
      });
    },
    baseFilter(a, b, c) {
      if (c > 0) {
        return c;
      } else {
        return "-";
      }
    },
    dateFilter(a, b, c) {
      if (c) {
        return base.timeSwitch(c);
      }
    },

    orderTypeFormatter(a, b, c) {
      for (let index = 0; index < this.orderTypeOptions.length; index++) {
        if (c == this.orderTypeOptions[index].orderTypeId) {
          return this.orderTypeOptions[index].orderType;
        }
      }
    },
    moneyFilter(a, b, c) {
      if (c) {
        return c;
      } else {
        return 0;
      }
    },
    orderStatusFormatter(a, b, c) {
      if (c == 0) {
        return "生成订单";
      } else if (c == 1) {
        return "订单处理中";
      } else {
        return "订单完成";
      }
    },
    isInvoiceFormatter(a, b, c) {
      if (c === 0) {
        return "未申请";
      } else if (c === 2) {
        return "已开票";
      } else if (c === 1) {
        return "已申请未开票";
      }
    }
  },
  filters: {
    dateFilter(c) {
      if (c) {
        return base.timeSwitch(c);
      }
    },
  },
  created() {
    this.getOrderType();
    this.getData();
  },
};
</script>
<style scoped>
.header {
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  font-size: 14px;
  border-bottom: 1px solid #eee;
}

.header i {
  margin-right: 5px;
  cursor: pointer;
}
.header span {
  cursor: pointer;
}
.opt-user {
  padding: 20px 15px;
}
.opt-user > section {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.opt-user i {
  font-size: 20px;
  margin-right: 5px;
  vertical-align: middle;
}
.opt-user .iconadd1 {
  color: #13cb6e;
}
.opt-user .iconbiaoge-xiazai {
  color: #33ac37;
}
.add-user:hover {
  color: #13cb6e;
}
.input-user:hover {
  color: #33ac37;
}
.menu-swtich {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #eee;
}
.menu-swtich li {
  display: inline-block;
  padding: 0 15px;
  cursor: pointer;
}
.active {
  color: #13cb6e;
  border-bottom: 2px solid #13cb6e;
}
.search-box {
  position: relative;
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #eee;
}
.search-box i {
  font-size: 18px;
  margin-left: 15px;
  color: #999;
  vertical-align: middle;
}
.search-box .search {
  display: inline-block;
  width: 80%;
}
.search-box .option {
  display: inline-block;
  margin-left: 15px;
}
.search-box .option .el-select {
  width: 150px;
}
.search-box .btn {
  position: absolute;
  right: 0;
  top: 0;
}
.showMoney {
  padding: 15px;
}
.showMoney .sm-h {
  margin-bottom: 20px;
}
.sm-h section {
  display: inline-block;
  margin-right: 20px;
}
.sm-h .month {
  margin-left: 10px;
  color: #999;
}
i {
  color: #999;
  font-size: 16px;
}
.download {
  float: right;
  font-size: 14px;
  margin-right: 0 !important;
  cursor: pointer;
}
.download:hover,
.download:hover i {
  color: #13cb6e !important;
}
.sm-f > ul {
  display: inline-block;
  margin-right: 15px;
  color: #666;
}
.sm-f .value {
  color: #666;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 5px;
}
.sm-f .no {
  opacity: 0;
}
.sm-f .button {
  float: right;
  width: 140px;
  text-align: center;
  height: 50px;
  font-size: 20px;
  line-height: 50px;
  color: #fff;
  cursor: pointer;
  border-radius: 4px;
  background-color: #13cb6e;
}
.table {
  margin-top: 4px;
}
.table .table-name {
  width: 100%;
  padding: 10px;
  background-color: #eee;
}
.table .table-value {
  width: 100%;
  padding: 10px;
  cursor: pointer;
}
.table .table-value:hover {
  background-color: #eee;
}
.table .table-name li,
.table .table-value li {
  position: relative;
  float: left;
  width: 20%;
  box-sizing: border-box;
}
.table .table-value h6 {
  color: #666;
  font-size: 14px;
}
.table .table-value p {
  color: #999;
  font-size: 12px;
}
.table .table-value .iconfont {
  position: absolute;
  top: 50%;
  right: 20px;
  margin-top: -8px;
  font-size: 16px;
}
.rests {
  float: right;
}
.el-form-item,
h4 {
  width: 390px;
  margin: 0 auto 22px;
}
.tip {
  width: 228px;
  text-align: center;
  background: #ccc;
  padding: 5px 10px;
  margin: 0 auto 22px;
  cursor: pointer;
}
.el-input,
.el-select {
  width: 228px;
}
h4 {
  color: #999;
}
.order-box li {
  text-align: center;
  line-height: 30px;
  cursor: pointer;
  margin-bottom: 2px;
  border-radius: 3px;
  background-color: #eee;
}
.order-box li:hover {
  color: #13cb6e;
}
.message {
  color: #666;
  text-align: center;
}
.file {
  margin-top: 10px;
  color: #999;
  text-align: center;
}
.billAmount-box {
  font-size: 14px;
  color: #999;
}
.billAmount {
  font-weight: 600;
  font-size: 16px;
  color: #333;
}
.img-head {
  position: relative;
  top: 10px;
  text-align: center;
  left: 50%;
  margin-left: -54px;
  width: 108px;
  height: 135px;
  cursor: pointer;
  background-color: #eee;
}
.img-head i {
  position: absolute;
  top: 50%;
  left: 50%;
  color: #999;
  font-size: 20px;
  margin-top: -10px;
  margin-left: -10px;
}
.img-head input {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  height: 100%;
  width: 100%;
  cursor: pointer;
}
</style>
